home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1995 October / EnigmA AMIGA RUN 01 (1995)(G.R. Edizioni)(IT)[!][issue 1995-10][Aminet 7].iso / Aminet / misc / emu / prlink_080b.lha / prlink-0.8.0b / src / prdisk.c < prev    next >
C/C++ Source or Header  |  1995-04-25  |  5KB  |  250 lines

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4.  
  5. #include "prtrans.h"
  6.  
  7. #if !defined(__MAIN_C__)
  8. #define main    main_prdisk
  9. #endif
  10.  
  11. #if defined(__MAIN_C__)
  12. unsigned char buffer[256];
  13. #else
  14. extern unsigned char buffer[];
  15. #endif
  16.  
  17. #define SECTORS     10
  18.  
  19. int main(int argc, char **argv);
  20.  
  21. int
  22. main(int argc, char **argv)
  23. {
  24.   FILE *file;
  25.   int device = 8;
  26.   int byte, i, writeflag = FALSE;
  27.   char **parameters = argv;
  28.   char *localfile = NULL;
  29.   char *prg = "prdisk.prg"; /* loader program file name */
  30.   int sectorcount = 0;
  31.   baseaddr = portaddr[DEFAULT_PORT];
  32.  
  33.   while (*++parameters && **parameters == '-') { /* check for options */
  34.     if (parameters[0][1] == '-') { /* "--" ends options */
  35.       parameters++;
  36.       break;
  37.     }
  38.  
  39.     switch (parameters[0][1]) {
  40.     case 'd':
  41.       device = strtoul (*++parameters, NULL, 0);
  42.  
  43.       if (device > 15) {
  44.     fprintf (stderr, "%s: Illegal device number specified.\n", *argv);
  45.     return 1;
  46.       }
  47.  
  48.       break;
  49.  
  50.     case 'r':
  51.       writeflag = FALSE;
  52.       break;
  53.     case 'w':
  54.       writeflag = TRUE;
  55.       break;
  56.  
  57.     case 'l':
  58.       prg = *++parameters;
  59.       break;
  60.  
  61.     case 'p':
  62.       baseaddr = strtoul (*++parameters, NULL, 16);
  63.  
  64.       if (baseaddr > 3) {
  65.     fprintf (stderr, "%s: The printer port number must be between 0 and 3.\n",
  66.              *argv);
  67.     return 1;
  68.       }
  69.  
  70.       baseaddr = portaddr[baseaddr];
  71.       break;
  72.  
  73.     case '?':
  74.     case 'h':
  75.     Usage:
  76.       fprintf (stderr, "%s: Copies a file to or from a disk accessible "
  77.                " from a remote computer.\n\n", *argv);
  78.       fprintf (stderr, "Usage: %s [options] [filename]\n",
  79.                *argv);
  80.       fprintf (stderr, "Options:\n\t"
  81.              "-d device\t"
  82.                  "Specify the device number (0-15)\n\t"
  83.              "-r\t"
  84.                  "Specify read operation (default)\n\t"
  85.              "-w\t"
  86.                  "Specify write operation\n\t"
  87.              "-l filename\t"
  88.                  "Specify the filename of the client program\n\t"
  89.              "-p port\t"
  90.                  "Specify the printer port (0 to 3)\n");
  91.       return 1;
  92.  
  93.     default:
  94.       fprintf (stderr, "%s: Illegal option `%s'.\n", *argv, *parameters);
  95.       goto Usage;
  96.     }
  97.   }
  98.  
  99.   if (*parameters) {
  100.     localfile = *parameters++;
  101.  
  102.     if (!(file = fopen(localfile, writeflag ? "rb" : "wb"))) {
  103.       fprintf (stderr, "%s: Could not open the file `%s'.\n", *argv,
  104.            localfile);
  105.       return 1;
  106.     }
  107.   }
  108.   else
  109.     file = writeflag ? stdin : stdout;
  110.  
  111.   {
  112.     int addr;
  113.  
  114.     FILE *f = fopen(prg, "r");
  115.     if (f == NULL) {
  116.       fprintf (stderr, "%s: Cannot find %s to download\n", *argv, prg);
  117.       return 1;
  118.     }
  119.  
  120.     addr = fgetc(f);
  121.     addr |= fgetc(f) << 8;
  122.     fclose(f);
  123.  
  124. #if defined(__MAIN_C__)
  125.     {
  126.       char cmd[80];
  127.       sprintf(cmd, "prload -j %x %s", addr, prg);
  128.  
  129.       fprintf(stderr, "%s\n", cmd);
  130.       system(cmd);
  131.     }
  132. #else
  133.     {
  134.       char saddr[10];
  135.       sprintf(saddr, "%x", addr);
  136.       vmain(main_prload, 4, "prload", "-j", saddr, prg, NULL);
  137.     }
  138. #endif
  139.   }
  140.  
  141.   if (prinit()) {
  142.     fprintf (stderr, "%s: Could not get the I/O permissions.\n", *argv);
  143.     return 1;
  144.   }
  145.  
  146.   if (wait_input ()) {
  147.     fprintf (stderr, "%s: not ready to transfer\n", *argv);
  148.     return 6;
  149.   }
  150.   else
  151.     fprintf (stderr, "%s: ok to transfer\n", *argv);
  152.  
  153.   output (writeflag ? 2 : 1); /* specify function: write or read a disk */
  154.   output (device); /* specify device number */
  155.  
  156.   if (wait_input ()) {
  157.     fprintf (stderr, "%s: I/O error on remote side\n", *argv);
  158.     fclose (file);
  159.     output (0);
  160.     prclose ();
  161.     return 7;
  162.   }
  163.  
  164.   if (writeflag) { /* loop to write all disk blocks */
  165.     while (!feof (file)) {
  166.  
  167.       sectorcount++;
  168.       if (sectorcount % SECTORS == 0) {
  169.     fprintf(stderr, " sector %d\r", sectorcount);
  170.     fflush(stderr);
  171.       }
  172.  
  173.       if (256 != fread (buffer + 1, 1, 256, file)) {
  174.     fprintf (stderr, "%s: Could not read all of the input file.\n", *argv);
  175.     break;
  176.       }
  177.       /* calculate the checksum */
  178.       for (*buffer = i = 0; i < 256; *buffer += buffer[++i]);
  179.  
  180.       output (0); /* notify start of block */
  181.  
  182.       for (;;) {
  183.     send (buffer, 257);
  184.  
  185.     if ((byte = wait_input ()) != 0x81)
  186.       break;
  187.  
  188.     fprintf(stderr, "%s: checksum error, retrying\n", *argv);
  189.       }
  190.  
  191.       if (byte != 0x80) {
  192.     fprintf (stderr, "%s: remote side aborted the transmission "
  193.              "with error code %u.\n", *argv, input ());
  194.     goto abort;
  195.       }
  196.     }
  197.  
  198.     output (1); /* end the transfer */
  199.   }
  200.   else { /* loop to get all disk blocks */
  201.     for (;;) {
  202.       for (;;) {
  203.     int chksum;
  204.  
  205.     sectorcount++;
  206.     if (sectorcount % SECTORS == 0) {
  207.       fprintf(stderr, " sector %d\r", sectorcount);
  208.       fflush(stderr);
  209.     }
  210.  
  211.     if ((byte = wait_input ())) {
  212.       if (byte == 66) /* illegal track or sector (end of disk) */
  213.         break;
  214.  
  215.       fprintf (stderr, "%s: disk error %u\n", *argv, byte);
  216.     abort:
  217.       fclose (file);
  218.       output (0);    /* terminate remote side */
  219.       prclose ();
  220.       return 7;
  221.     }
  222.  
  223.     receive(buffer, 257);
  224.  
  225.     for (chksum = i = 0; i < 256; chksum += buffer[++i]);
  226.  
  227.     if ((chksum & 0xFF) == buffer[0]) {
  228.       output(0x80);
  229.       break;
  230.     }
  231.     fprintf(stderr, "checksum error "
  232.             "(received %02x, calculated %02x), retrying\n",
  233.         buffer[0], (chksum & 0xFF));
  234.     output(0x81);
  235.       }
  236.  
  237.       if (byte == 66)
  238.     break; /* end of disk */
  239.  
  240.       fwrite (buffer + 1, 1, 256, file);
  241.     }
  242.   }
  243.  
  244.   fclose (file);
  245.  
  246.   output (0);    /* terminate remote side */
  247.   prclose ();
  248.   return 0;
  249. }
  250.